package com.egao.orderB.service.impl;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.PhoneUtils;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.biz.UserBiz;
import com.egao.common.system.entity.User;
import com.egao.common.system.service.UserService;
import com.egao.orderB.biz.CustomerBiz;
import com.egao.orderB.biz.InvoiceNameBiz;
import com.egao.orderB.entity.Customer;
import com.egao.orderB.entity.CustomerCopy;
import com.egao.orderB.entity.InvoiceName;
import com.egao.orderB.service.CustomerService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

//import com.egao.orderB.mapper.CustomerMapper;
//import javax.transaction.Transactional;

/**
 * 货代管理服务实现类
 * @author : Doman
 * @date : 2020-06-02 13:34:13
 * @version ：1.0
 */
@Service
public class CustomerServiceImpl /*extends ServiceImpl<CustomerMapper, Customer> */implements CustomerService {

	@Autowired
	private CustomerBiz customerBiz;
	@Autowired
	private UserBiz userBiz;
    @Autowired
    private UserService userService;
    @Autowired
    private InvoiceNameBiz invoiceNameBiz;

    /**
    @Override
    public PageResult<Customer> listPage(PageParam<Customer> page) {
        List<Customer> records = baseMapper.listPage(page);
        return new PageResult<>(records, page.getTotal());
    }

    @Override
    public List<Customer> listAll(Map<String, Object> page) {
        return baseMapper.listAll(page);
    } */

    @Override
    public JsonResult list(PageResult2<T> page, Customer customer, User loginUser) throws Exception {
        return customerBiz.list(page,customer,loginUser);
    }

    @Override
    public JsonResult list(Customer customer, User loginUser) {
//        List<Customer> list = customerBiz.list(customer,loginUser);
//        for(Customer f: list) {
//            if(f.getPinyin()==null || f.getPinyin().equals(""))
//                customerBiz.update2(f,loginUser);
//        }
        return JsonResult.ok().setData(customerBiz.list(customer,loginUser));
    }

    @Override
    public Customer getById(Customer customer) {
        return customerBiz.getById(customer);
    }

    @Override
    public JsonResult getByName(Customer customer) {
        try {
            List<Customer> list  = customerBiz.getByName(customer);
            if(list != null && list.size() > 0) {
                return JsonResult.ok().setData(customerBiz.getByName(customer).get(0));
            } else {
                return JsonResult.error();
            }
        } catch (Exception e) {
            return JsonResult.error();
        }
    }

    @Transactional
    @Override
    public JsonResult save(Customer customer, User loginUser) {
        if(Utils.isEmpty(customer.getName())|| Utils.isEmpty(customer.getType())
        ){
            return JsonResult.error("请填写公司名称,客户类型,操作失败!");
        }
        if (customerBiz.save(customer, loginUser)) {
            return JsonResult.ok("添加成功");
        }
        return JsonResult.error("添加失败");
    }

    @Transactional
    @Override
    public JsonResult ApiSaveWidthMessageCode(CustomerCopy customer, User loginUser, User user) throws Exception {
        //蔡总要求去掉验证码 注册
//        if(Utils.isEmpty(user.getMessageCode())
//        ){
//            return JsonResult.error("请填写验证码,操作失败!");
//        }
//        String messageCode2 = CacheUtils.timedCache.get(user.getUsername());
//        if(!user.getMessageCode().equals(messageCode2)){
//            return JsonResult.error("验证码不对!");
//        }
        return ApiSave(customer, loginUser,user);
    }

    @Transactional
    @Override
    public JsonResult ApiSave(CustomerCopy customer, User loginUser, User user) throws Exception {
        if(Utils.isEmpty(user.getUsername())||Utils.isEmpty(customer.getType())||
                Utils.isEmpty(customer.getName())||Utils.isEmpty(user.getPhone())
        ){
            return JsonResult.error("请填写完整,操作失败!");
        }
        customer.setOrganizationId(UserConstant.ORGANIZATION_ID + "");
        if (Utils.isEmpty(customer.getState())) {
            customer.setState("未审核");
        }
        if (Utils.isEmpty(customer.getSettle())) {
            customer.setSettle("月结");
        }
        customer.setAdminUser(user.getUsername());
        Customer cc = new Customer();
        cc.setName(customer.getName());
        List<Customer> customerList = customerBiz.getByName(cc);
        if(customerList != null && customerList.size() > 0) {
            return JsonResult.error("注册失败,名称已经注册!");
        }
        int key = 0;
        try {
            key = customerBiz.ApiSave(customer, loginUser);
        } catch (DataIntegrityViolationException e) {
            e.printStackTrace();
            throw new Exception("日期格式不正确");
        }
        user.setOrganizationId(UserConstant.ORGANIZATION_ID);
        user.setCustomerId(key);
        user.setType(customer.getType());
        user.setAdmin("1");
        user.setNickName(customer.getName());
        if(customer.getState().equals("已审核")){
            user.setState("0");
        } else {
            user.setState("2");
        }
        user.setPasswordorigin(user.getPassword());
        user.setPassword(userService.encodePsw(user.getPassword()));
        key= userService.apiSave(user);
        user = new User();
        user.setUserId(key);
        if (customer.getType().equals(UserConstant.CUSTOMER_NAME)) {
            user.setRoleId(UserConstant.CUSTOMER_ROLE_ID);
        } else {
            user.setRoleId(UserConstant.DRIVER_ROLE_ID);
        }
        userService.saveUserRole(user);
        InvoiceName invoiceName = new InvoiceName();
        invoiceName.setName(customer.getName());
        invoiceName.setType(customer.getType());
        invoiceName.setState("0");
        invoiceNameBiz.save(invoiceName,loginUser);
        return JsonResult.ok("注册成功");
    }

    @Override
    public JsonResult update(Customer customer, User loginUser) {
        if (customerBiz.update(customer, loginUser)) {
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }

    @Override
    public JsonResult updateInvoiceState(Customer customer, User loginUser) {
        if (customerBiz.updateInvoiceState(customer, loginUser)) {
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }

    @Override
    public JsonResult updateMessage(Customer customer, User loginUser) {
        if (customerBiz.updateMessage(customer, loginUser)) {
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }

    @Override
    public JsonResult updateGzhSend(Customer customer, User loginUser) {
        if (customerBiz.updateGzhSend(customer, loginUser)) {
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }

    @Override
    @Transactional
    public JsonResult remove(Customer customer, User loginUser) {
        Customer byId = customerBiz.getById(customer);
        User user = new User();
        user.setUsername(byId.getAdminUser());
        user = userBiz.apiGetByUsername(byId.getAdminUser());
        userBiz.remove(user,loginUser);
        if (customerBiz.remove(customer, loginUser)) {
            return JsonResult.ok("删除成功");
        }
        return JsonResult.error("删除失败");
    }

    @Transactional
    @Override
    public JsonResult examine(Customer customer, User loginUser) {
        Customer customer2 = customerBiz.getById(customer);
        User user = userService.apiGetByUsername(customer2.getAdminUser());
        user.setState("0");
        userBiz.apiExamine(user, loginUser);
        customer.setState("已审核");
        if (customerBiz.examine(customer, loginUser)) {
            PhoneUtils.sendMsg(user.getUsername(), "600424", new String[]{user.getUsername()},1);
            return JsonResult.ok("审核成功");
        }
        return JsonResult.error("审核失败");
    }

    @Transactional
    @Override
    public JsonResult examineFail(Customer customer, User loginUser) {
        if (customerBiz.examineFail(customer, loginUser)) {
            return JsonResult.ok("审核成功");
        }
        Customer customer2 = customerBiz.getById(customer);
        PhoneUtils.sendMsg(customer2.getAdminUser(), "600431", new String[]{customer2.getAdminUser()},1 );
        return JsonResult.error("审核失败");
    }

}
